package collect.taobao.dao.impl;
import collect.taobao.dao.CartoonStatDao;
import collect.taobao.bean.CartoonStatBean;


import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

public class CartoonStatDaoImpl extends HibernateDaoSupport implements CartoonStatDao{
	public int save(CartoonStatBean cartoonStatBean) {
		int i=0;
		Session session =null;
		try{
			session = this.getSession();
	        Transaction trans = session.beginTransaction();
			this.getHibernateTemplate().save(cartoonStatBean);
			trans.commit();   
			i=1;
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			if(session!=null)session.close();
		}
		return i;
	}
	public int delete(CartoonStatBean cartoonStatBean) {
		int i=0;
		Session session =null;
		try{
			session = this.getSession();
	        Transaction trans = session.beginTransaction();
			this.getHibernateTemplate().delete(cartoonStatBean);
			trans.commit();   
			i=1;
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			if(session!=null)session.close();
		}
		return i;
	}

	public int update(CartoonStatBean cartoonStatBean) {
		int i=0;
		Session session =null;
		try{
			session = this.getSession();
	        Transaction trans = session.beginTransaction();
			this.getHibernateTemplate().update(cartoonStatBean);
			trans.commit();   
			i=1;
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			if(session!=null)session.close();
		}
		return i;
	}
	
	public List queryList() {
		return this.getHibernateTemplate().loadAll(CartoonStatBean.class);
	}

	public CartoonStatBean query(String id) {
		Session session =null;
		CartoonStatBean  cartoonStatBean = null;
		try{
			session = this.getSession();
			cartoonStatBean = (CartoonStatBean)session.load(CartoonStatBean.class, new Integer(id)); 
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			if(session!=null)session.close();
		}
		return cartoonStatBean;
	}
	public List queryListByClassification(String classification) {
		String queryString = "from CartoonStatBean where classification='"+ classification +"'";
		return this.getHibernateTemplate().find(queryString);
	}
	public List queryListByName(String name) {
		String queryString = "from CartoonStatBean where name='"+ name +"'";
		return this.getHibernateTemplate().find(queryString);
	}
	public List queryListOrderByclicks() {
		String queryString = "from CartoonStatBean order by clicks DESC";
		return this.getHibernateTemplate().find(queryString);
	}
}
